﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuestionType = Zebra.Umbraco.Marcos.Code.QuestionType;

namespace Zebra.Umbraco.Marcos.Survey.QuestionEditor
{
    public partial class RegularQuestion : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";

            if (Request.HttpMethod.ToLower() == "post")
            {
                if (Request["QuestionID"] == null)
                {
                    if (Request["Question_Title"] != null
                        && Request["Question_Choices"] != null
                        && Request["Question_SortingMethod"] != null
                        && Request["Question_MultiSelect"] != null
                        && Request["Question_MustAnswer"] != null
                        && Request["Question_AllowComment"] != null
                         && Request["SurveyID"] != null
                        )
                    {
                        try
                        {
                            QuestionType.RegularQuestion question = new QuestionType.RegularQuestion()
                            {
                                QuestionText = Request["Question_Title"],
                                AnswerText = Request["Question_Choices"],
                                SurveyID = int.Parse(Request["SurveyID"])
                            };

                            question.SortType = ((QuestionType.SortType)int.Parse(Request["Question_SortingMethod"]));
                            bool tryValue;
                            if (bool.TryParse(Request["Question_MultiSelect"], out tryValue))
                            {
                                question.MultipleSelection = tryValue;
                            }
                            if (bool.TryParse(Request["Question_MustAnswer"], out tryValue))
                            {
                                question.MustAnswer = tryValue;
                            }
                            if (bool.TryParse(Request["Question_AllowComment"], out tryValue))
                            {
                                question.AllowComment = tryValue;
                            }
                            using (DataClasses1DataContext db = new DataClasses1DataContext())
                            {
                                surveyQuestion sQuestion = new surveyQuestion()
                                {
                                    questionXML = question.ToXml(),
                                    surveyID = int.Parse(Request["SurveyID"]),
                                    questionType = Enum.GetName(typeof(QuestionType.QuestionCatalog), question.Catalog),
                                    surveyPage = 1,
                                    RowCreatedOn = DateTime.Now,
                                    RowModifiedOn = DateTime.Now,
                                };
                                db.surveyQuestions.InsertOnSubmit(sQuestion);
                                db.SubmitChanges();
                                question.QuestionID = sQuestion.surveyQuestionID;
                                sQuestion.questionXML = question.ToXml();
                                db.SubmitChanges();
                            }
                            Response.Write(question.GenerateHTML(true));
                        }
                        catch (Exception ex)
                        {
                            Response.Write(ex.Message);
                            Response.Write(ex.StackTrace);
                        }
                        Response.End();
                    }
                }
                else
                {
                    if (Request["Question_Title"] != null
                        && Request["Question_Choices"] != null
                        && Request["Question_SortingMethod"] != null
                        && Request["Question_MultiSelect"] != null
                        && Request["Question_MustAnswer"] != null
                        && Request["Question_AllowComment"] != null
                         && Request["SurveyID"] != null
                        )
                    {

                        try
                        {
                            using (DataClasses1DataContext db = new DataClasses1DataContext())
                            {
                                surveyQuestion sQuestion = db.surveyQuestions.First(c => c.surveyID == int.Parse(Request["SurveyID"]) && c.surveyQuestionID == int.Parse(Request["QuestionID"]));
                                QuestionType.RegularQuestion question = (QuestionType.RegularQuestion)QuestionType.Question.FromXML(sQuestion.questionType, sQuestion.questionXML);

                                question.QuestionText = Request["Question_Title"];
                                question.AnswerText = Request["Question_Choices"];

                                question.SortType = ((QuestionType.SortType)int.Parse(Request["Question_SortingMethod"]));
                                bool tryValue;
                                if (bool.TryParse(Request["Question_MultiSelect"], out tryValue))
                                {
                                    question.MultipleSelection = tryValue;
                                }
                                if (bool.TryParse(Request["Question_MustAnswer"], out tryValue))
                                {
                                    question.MustAnswer = tryValue;
                                }
                                if (bool.TryParse(Request["Question_AllowComment"], out tryValue))
                                {
                                    question.AllowComment = tryValue;
                                }
                                sQuestion.questionXML = question.ToXml();
                                sQuestion.RowModifiedOn = DateTime.Now;
                                db.SubmitChanges();
                                Response.Write(question.GenerateHTML(true));
                            }

                        }
                        catch (Exception ex)
                        {
                            Response.Write(ex.Message);
                            Response.Write(ex.StackTrace);
                        }
                        Response.End();
                    }
                }
            }
            else
            {
                if (Request["QuestionID"] != null)
                {
                    if (Request["Delete"] != null)
                    {
                      
                        using (DataClasses1DataContext db = new DataClasses1DataContext())
                        {
                            surveyQuestion sQuestion = db.surveyQuestions.First(c => c.surveyID == int.Parse(Request["SurveyID"]) && c.surveyQuestionID == int.Parse(Request["QuestionID"]));
                            db.surveyQuestions.DeleteOnSubmit(sQuestion);
                            db.SubmitChanges();
                          
                        }
                        Response.End();
                    }
                    else
                    {
                        
                        using (DataClasses1DataContext db = new DataClasses1DataContext())
                        {
                            surveyQuestion sQuestion = db.surveyQuestions.First(c => c.surveyID == int.Parse(Request["SurveyID"]) && c.surveyQuestionID == int.Parse(Request["QuestionID"]));
                            QuestionType.RegularQuestion question = (QuestionType.RegularQuestion)QuestionType.Question.FromXML(sQuestion.questionType, sQuestion.questionXML);
                            this.Question_Title.InnerText = question.QuestionText;
                            this.Question_Choices.InnerText = question.AnswerText;
                            this.Question_AllowComment.Checked = question.AllowComment;
                            this.Question_MultiSelect.Checked = question.MultipleSelection;
                            this.Question_MustAnswer.Checked = question.MustAnswer;
                            this.Question_SortingMethod.SelectedIndex = (int)question.SortType;
                        }
                       
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            //QuestionType.RegularQuestion question = new QuestionType.RegularQuestion()
            //{
            //    QuestionText = txtQuestionText.Text.Trim(),
            //    AllowComment = cbAddComment.Checked,
            //    MultipleSelection = cbMultiSelect.Checked,
            //    MustAnswer = cbMustAnswer.Checked,
            //    SortType = (QuestionType.SortType)(int.Parse(ddlSortType.SelectedValue))
            //};
            //using (DataClasses1DataContext db = new DataClasses1DataContext())
            //{
            //    //surveyQuestion sQuestion = new surveyQuestion()
            //    //{
            //    //    QuestionXML = question.ToXml(),
            //    //    SurveyID = 1
            //    //};
            //    //db.surveyQuestions.InsertOnSubmit(sQuestion);
            //    //db.SubmitChanges();
            //}
        }
    }
}